package com.example.hdwx.myapplication.json;

/**
 * Created by hdwx on 2017/2/21.
 */




        import com.example.hdwx.myapplication.eneity.goodChoice_eneitys.CyclicGraphEneity;
        import com.example.hdwx.myapplication.eneity.goodChoice_eneitys.ImagUrlEneity;
        import com.example.hdwx.myapplication.eneity.goodChoice_eneitys.LargeFragmentsEneity;
        import com.example.hdwx.myapplication.eneity.goodChoice_eneitys.SmallFragmentsDetailsEneity;
        import com.example.hdwx.myapplication.eneity.goodChoice_eneitys.SmallFragmentsEneity;

        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;

        import java.util.ArrayList;
        import java.util.List;

/**
 * Created by hudongwx on 17-2-13.
 * 优选碎片的json类
 */

public class GoodChoiceJson {

    public static List<CyclicGraphEneity> getContent(String response,int start,int end){
        try {
            JSONObject jsObject = new JSONObject(response);
            JSONObject data = jsObject.getJSONObject("data");
            JSONArray home_list = data.getJSONArray("home_list");

            //存放所有网络数据集合
            List<CyclicGraphEneity> cyclicGraph = new ArrayList<>();
            CyclicGraphEneity graphEneity = null;
            //存放优选中的循环图片
            List<ImagUrlEneity> imagUrl = new ArrayList<>();
            ImagUrlEneity imaEneity = null;

            LargeFragmentsEneity larFragment = null;
            SmallFragmentsEneity smaFragment = null;
            SmallFragmentsDetailsEneity smallFragmentDetails = null;

            int end1;
            if (home_list.length()-end<=10){
                end1 = home_list.length();
            }else {
                end1 = end;
            }
            for (int i = start; i < end1; i++) {
                graphEneity = new CyclicGraphEneity();

                JSONObject jsObjectI = home_list.getJSONObject(i);
                //存放大item相关数据
                List<LargeFragmentsEneity> largeFragments = new ArrayList<>();

                if (i==0){
                    JSONArray banners = jsObjectI.getJSONArray("banners");
                    for (int j = 0; j < banners.length(); j++) {
                        JSONObject jsObjectJ = banners.getJSONObject(j);
                        imaEneity = new ImagUrlEneity();
                        imaEneity.setImage_url(jsObjectJ.optString("image_url"));
                        imagUrl.add(imaEneity);
                    }
                }else if (i>1){

                    JSONObject post = jsObjectI.getJSONObject("post");
                    larFragment = new LargeFragmentsEneity();
                    larFragment.setTitle(post.optString("title"));
                    larFragment.setChannel_title(post.optString("channel_title"));
                    larFragment.setNew_cover_image_url(post.optString("new_cover_image_url"));
                    larFragment.setUrl(post.optString("url"));
                    larFragment.setLikes_count(post.optString("likes_count"));

                    JSONArray post_items = post.getJSONArray("post_items");
                    //存放小item相关数据
                    List<SmallFragmentsEneity> smallFragments = new ArrayList<>();
                    for (int j = 0; j < post_items.length(); j++) {

                        JSONObject jsObjectJ = post_items.getJSONObject(j);
                        smaFragment = new SmallFragmentsEneity();
                        smaFragment.setCover_image_url(jsObjectJ.optString("cover_image_url"));
                        smaFragment.setDescription(jsObjectJ.optString("description"));
                        smaFragment.setPrice(jsObjectJ.optString("price"));
                        smaFragment.setUrl(jsObjectJ.optString("url"));

                        JSONArray image_urls = jsObjectJ.getJSONArray("image_urls");
                        //存放小item详情中的循环图片网址
                        List<SmallFragmentsDetailsEneity> smallFragmentsDetails = new ArrayList<>();
                        for (int k = 0; k < image_urls.length(); k++) {

                            String o = (String) image_urls.get(k);
                            smallFragmentDetails = new SmallFragmentsDetailsEneity();
                            smallFragmentDetails.setImage_urls(o);

                            smallFragmentsDetails.add(smallFragmentDetails);
                        }
                        smaFragment.setList(smallFragmentsDetails);

                        smallFragments.add(smaFragment);
                    }
                    larFragment.setList(smallFragments);

                    largeFragments.add(larFragment);
                }
                graphEneity.setImagList(imagUrl);
                graphEneity.setList(largeFragments);
                cyclicGraph.add(graphEneity);//添加到集合
            }
            return cyclicGraph;

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}

